/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.MaintainPartsAttributeCategoryDeleteBatchDTO;
import org.springblade.modules.admin.entity.MaintainPartsAttribute;
import org.springblade.modules.admin.service.IMaintainPartsAttributeService;
import org.springblade.modules.admin.vo.MaintainPartsAttributeCategorySelectVO;
import org.springframework.web.bind.annotation.*;
import org.springblade.modules.admin.entity.MaintainPartsAttributeCategory;
import org.springblade.modules.admin.service.IMaintainPartsAttributeCategoryService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 000000 租户
 * 配件_属性_类别表 控制器
 * @author ysq
 * @since 2024-10-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("sida-maintain-admin/maintain/parts/attribute/category")
@Api(value = "配件_属性_类别表", tags = "配件_属性_类别表接口")
public class MaintainPartsAttributeCategoryController extends BladeController {

	private final IMaintainPartsAttributeCategoryService maintainPartsAttributeCategoryService;

	private final IMaintainPartsAttributeService maintainPartsAttributeService;

	/**
	 * 新增或修改配件维保属性类别
	 */
	@TenantIgnore
	@PostMapping("/save-or-edit")
	@ApiOperation("新增配件维保属性类别")
	@ApiOperationSupport(order = 10)
	public R<Long> addOne(@RequestBody MaintainPartsAttributeCategory category) {
		// 1. 租户验证、参数验证
		if(isNotAdminTenant()){
			throw new ServiceException("请在000000租户进行配件维保属性类别配置");
		}
		String categoryName = category.getAttributeCategoryName();
		if(Func.isNull(categoryName) || categoryName.trim().isEmpty()){
			throw new ServiceException("属性类别名称不能为空或空串");
		}
		// 2. 查看是否存在相同名称

		List<MaintainPartsAttributeCategory> existList = maintainPartsAttributeCategoryService.list();
		if(!Func.isEmpty(existList)){
			List<String> existNameList = existList
				.stream()
				.map(MaintainPartsAttributeCategory::getAttributeCategoryName)
				.collect(Collectors.toList());
			if(existNameList.contains(categoryName)){
				throw new ServiceException("输入维保类别属性已存在，不可重复录入");
			}
		}

		// 3. 调用service实现新增或修改
		category.setTenantId(AuthUtil.getTenantId());
		maintainPartsAttributeCategoryService.saveOrUpdate(category);
		return R.data(category.getId());
	}

	/**
	 * 删除配件维保属性类别
	 */
	@TenantIgnore
	@PostMapping("/delete/batch")
	@ApiOperationSupport(order = 20)
	@ApiOperation(value = "删除配件维保属性类别", notes = "传入id列表ids")
	public R<Boolean> deleteBatch(@Valid @RequestBody MaintainPartsAttributeCategoryDeleteBatchDTO deleteBatchDTO) {
		// 1. 租户验证
		if(isNotAdminTenant()){
			throw new ServiceException("请在000000租户进行配件维保属性类别配置");
		}
		// 2. 查询当前id列表是否均已存在关联属性
		List<Long> ids = deleteBatchDTO.getIds();
		ids.forEach(
			id -> {
				List<MaintainPartsAttribute> existList = maintainPartsAttributeService.list(
					new LambdaQueryWrapper<MaintainPartsAttribute>()
						.eq(MaintainPartsAttribute::getAttributeCategoryId, id)
						.eq(BaseEntity::getIsDeleted,0)
				);
				if(Func.isNotEmpty(existList)){
					throw new ServiceException("即将删除的类别下存在关联维保属性，请先删除已关联的属性!");
				}
			}
		);
		return R.status(maintainPartsAttributeCategoryService.removeByIds(deleteBatchDTO.getIds()));
	}


	/**
	 * 配件维保属性选择列表
	 */
	@TenantIgnore
	@GetMapping("/select/list")
	@ApiOperationSupport(order = 30)
	@ApiOperation(value = "配件维保属性选择列表", notes = "返回配件维保属性选择列表")
	public R<List<MaintainPartsAttributeCategorySelectVO>> selectList() {
		if(isNotAdminTenant()){
			throw new ServiceException("请在000000租户进行配件维保属性配置");
		}
		return R.data(maintainPartsAttributeCategoryService.selectList());
	}

	private boolean isNotAdminTenant() {
		String adminTenantId = AuthUtil.getTenantId();
		return !"000000".equals(adminTenantId);
	}


}
